package com.controller
{
	import com.events.ProductMasterEvent;
	import com.model.valueObjects.VOProductImage;
	import com.model.valueObjects.VOProductMaster;
	import com.service.ProductMaster;
	
	import flash.events.Event;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.utils.Base64Decoder;

	public class ProductMasterController extends AbstractController
	{
		[Inject]
		public var productMasterService:ProductMaster;
		
		public function ProductMasterController()
		{
			super();
		}		
		
		[EventHandler(event="ProductMasterEvent.GET_PRO_NAME")]
		public function getProName():void{
			sh.executeServiceCall(productMasterService.getDitinctProName(),getProNameResult, handleFault );
		}
		private function getProNameResult(event:ResultEvent):void
		{
			model.proNameArr =event.result as ArrayCollection;
		}
		
		[EventHandler(event="ProductMasterEvent.GET_PART_NO")]
		public function getPartNo():void{
			sh.executeServiceCall(productMasterService.getDitinctPartNo(),getPartNoResult, handleFault );
		}
		private function getPartNoResult(event:ResultEvent):void
		{
			model.partNoArr =event.result as ArrayCollection;
		}
		
		[EventHandler(event="ProductMasterEvent.GET_STAMP_NO")]
		public function getStampNo():void{
			sh.executeServiceCall(productMasterService.getDitinctStampNo(),getStampNoResult, handleFault );
		}
		private function getStampNoResult(event:ResultEvent):void
		{
			model.stampNoArr =event.result as ArrayCollection;
		}
		
		[EventHandler(event="ProductMasterEvent.GET_IMAGE",properties="imgSerialnumber")]
		public function getImage(imgSerialnumber:Number):void{
			model.productImage = model.loadingImageByteArr;
			sh.executeServiceCall(productMasterService.getProductImage(imgSerialnumber),getImageResult, handleFault );
		}		
		private function getImageResult(event:ResultEvent):void
		{
			var imageVO:VOProductImage=event.result as VOProductImage;
			var byteArr:ByteArray;
			var base64Dec:Base64Decoder = new Base64Decoder();
			var str:String=String(imageVO.product_image);
			base64Dec.decode(str);
			byteArr = base64Dec.toByteArray();
			model.productImage= byteArr;
		}
		
		[EventHandler(event="ProductMasterEvent.GET_ALL")]
		public function getAll():void{
			model.currentAdminTableIndex=3;
			sh.executeServiceCall(productMasterService.getAllProduct(),getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{
			model.productMasterList =event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="ProductMasterEvent.GET_LIST")]
		public function getAllList():void{
			sh.executeServiceCall(productMasterService.getProductList(),getListAllResult, handleFault );
		}
		private function getListAllResult(event:ResultEvent):void
		{
			model.productList =event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn = true;
		}
		
		[EventHandler(event="ProductMasterEvent.GET_LIST_SEARCH")]
		public function getProductListSearch():void{
			sh.executeServiceCall(productMasterService.getProductListOnSearch(),getProductListSearchResult, handleFault );
		}
		private function getProductListSearchResult(event:ResultEvent):void
		{
			model.productListSearch =event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="ProductMasterEvent.GET_LIST_WITH_PRICE")]
		public function getAllListWithPrice():void{
			sh.executeServiceCall(productMasterService.getProductListWithPrice(),getAllListWithPriceResult, handleFault );
		}
		private function getAllListWithPriceResult(event:ResultEvent):void
		{
			model.productListWithPrice =event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="ProductMasterEvent.ADD", properties="productMastervo,imageData")]
		public function add(productMastervo:VOProductMaster,imageData:String):void
		{
			sh.executeServiceCall(productMasterService.addNewProduct(productMastervo,imageData), addResult, handleFault );	
		}		
		private function addResult(event:ResultEvent):void
		{
			model.productMasterList=event.result as ArrayCollection;
			
			if(model.saveCloseBtn)model.currentForm=1;

			getAll();
			getProName();
			getPartNo();
			getStampNo();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("New Product Added","Product" );
		}
		
		[EventHandler(event="ProductMasterEvent.UPDATE", properties="productMastervo,imageData")]
		public function updateCompany(productMastervo:VOProductMaster,imageData:String):void
		{
			sh.executeServiceCall(productMasterService.editProduct(productMastervo,imageData), updateResult, handleFault );	
		}
		private function updateResult(event:ResultEvent):void
		{
			model.currentForm=1;
			getAll();
			getProName();
			getPartNo();
			getStampNo();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Details Updated ","Product");
		}
		
		[EventHandler(event="ProductMasterEvent.DELETE", properties="serialnumber")]
		public function deleteR(serialnumber:int):void
		{
			sh.executeServiceCall(productMasterService.deleteProduct(serialnumber), deleteResult, handleFault );	
		}
		private function deleteResult(event:ResultEvent):void
		{
			model.currentForm=1;
			getAll();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Record Deleted ","Product" );
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}